package com.lucweb.action;


import com.lucweb.entity.BaseDict;
import com.lucweb.entity.TaskTell;
import com.lucweb.service.BaseDictService;

import com.lucweb.service.TellService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Controller("tellAction")
@Scope("prototype")
public class TellAction extends ActionSupport{

    @Resource
    private TellService tellService;

    @Resource
    private BaseDictService baseDictService;

    private File file;
    //文件名称
    private String fileFileName;

    //文件类型
    private String fileContentType;
    //注意：文件名称和文件类型的名称前缀必须相同，

    private InputStream fileInput;



    private TaskTell taskTell;

    private int taskId;

    //新建接电窗口
    public String newTell(){

        //没有id时设置id
        int bid = tellService.getmaxId();

        List<BaseDict> nameList=baseDictService.getListByTypeCode("3");
        List<BaseDict>companyList=baseDictService.getListByTypeCode("2");

        ActionContext.getContext().put("nameList",nameList);
        ActionContext.getContext().put("bid",bid);
        ActionContext.getContext().put("companyList",companyList);


        return "tells";

    }

    //修改接电
    public String editTell(){

        taskTell = tellService.gettasktellById(taskId);

        ActionContext.getContext().put("taskTell", taskTell);

        List<BaseDict>nameList=baseDictService.getListByTypeCode("3");
        List<BaseDict>companyList=baseDictService.getListByTypeCode("2");

        ActionContext.getContext().put("nameList",nameList);
        ActionContext.getContext().put("companyList",companyList);




        System.out.println(taskTell);



        return "tells";

    }

    //删除接电
    public String deleteTell(){

        tellService.deletetelltask(taskId);

        //刷新页面
        List<TaskTell>tellList=tellService.showtell();
        ActionContext.getContext().put("tellList",tellList);

        return "addtell";

    }

    //接电
    public String saveOrUpdate(){

        System.out.println(taskTell);

        File uploadFile=new File(ServletActionContext.getServletContext().getRealPath("uploadFile"));
        //判断文件是否上传，如果上传的话将会创建该目录
        if(!uploadFile.exists()){
            uploadFile.mkdir(); //创建该目录
        }


        try {
            if(file!=null){
                FileUtils.copyFile(file,new File(uploadFile,fileFileName));
            }

        } catch (IOException e) {
            e.printStackTrace();
        }





        if(taskTell.getUpdown().length()==0){
            if(fileFileName!=null){
                taskTell.setUpdown(fileFileName);
            }else {
                taskTell.setUpdown(null);
            }

        }else {
            if(fileFileName!=null){
                taskTell.setUpdown(fileFileName);
            }

        }




        //先保存
        tellService.saveOrUpdate(taskTell);

        //刷新页面
        List<TaskTell>tellList=tellService.showtell();
        ActionContext.getContext().put("tellList",tellList);

        return "addtell";

    }

    //获取接电列表
    public String tellshow(){


        List<TaskTell>tellList=tellService.showtell();
        ActionContext.getContext().put("tellList",tellList);

        return "addtell";

    }


    public TellService getTellService() {
        return tellService;
    }

    public void setTellService(TellService tellService) {
        this.tellService = tellService;
    }

    public BaseDictService getBaseDictService() {
        return baseDictService;
    }

    public void setBaseDictService(BaseDictService baseDictService) {
        this.baseDictService = baseDictService;
    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public String getFileFileName() {
        return fileFileName;
    }

    public void setFileFileName(String fileFileName) {
        this.fileFileName = fileFileName;
    }

    public String getFileContentType() {
        return fileContentType;
    }

    public void setFileContentType(String fileContentType) {
        this.fileContentType = fileContentType;
    }

    public InputStream getFileInput() {
        return fileInput;
    }

    public void setFileInput(InputStream fileInput) {
        this.fileInput = fileInput;
    }

    public TaskTell getTaskTell() {
        return taskTell;
    }

    public void setTaskTell(TaskTell taskTell) {
        this.taskTell = taskTell;
    }


    public int getTaskId() {
        return taskId;
    }

    public void setTaskId(int taskId) {
        this.taskId = taskId;
    }


}
